package com.example.userwallet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.userwallet.entity.*;
import com.example.userwallet.mapper.*;
import com.example.userwallet.service.OrderItemsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.userwallet.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 交易详情表 服务实现类
 * </p>
 *
 * @author lyl
 * @since 2023-03-15
 */
@Service
public class OrderItemsServiceImpl extends ServiceImpl<OrderItemsMapper, OrderItems> implements OrderItemsService {

    @Autowired(required = false)
    OrderItemsMapper orderItemsMapper;
    @Autowired(required = false)
    AccountMapper accountMapper;
    @Autowired(required = false)
    WalletMapper walletMapper;
    @Autowired(required = false)
    COrdersMapper cOrdersMapper;

    @Autowired(required = false)
    ROrdersMapper rOrdersMapper;
    /**
     * 用户消费100元的service
     * @param userAccount 用户账号
     * @param amount 消费金额
     * @param merchantAccount 商户账号 暂时没有商户表
     *                        此处可以接入其他的接口完成商户和用户之间的转账
     *                        可以选择对应商家账号对应的银行账户进行转账
     * @return  结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public JsonUtil AccConsume(String userAccount, Double amount, String merchantAccount) {
        JsonUtil jsonUtil = new JsonUtil();

        //判断用户账号是否存在
        List<Account> userList = accountMapper.selectList(
                new QueryWrapper<Account>()
                    .select("user_account")
                    .eq("user_account",userAccount)
                        .eq("del_flag",0));
        //商户账号有专用表，可以有对应的接口，暂不做校验
        if(userList!=null && userList.size()>0){
            //用户存在判断账号余额
            Wallet wallet = walletMapper.selectOne(
                    new QueryWrapper<Wallet>()
                            .select("amount")
                            .eq("user_account",userAccount)
                            .eq("status",1)
            );
            if(wallet.getAmount() > amount){
                //账户余额充足
                UpdateWrapper<Wallet> updateWrapper = new UpdateWrapper<>();
                updateWrapper
                        .eq("user_account", userAccount)
                        .eq("status",1); // 设置更新条件
                 Wallet wallet1 = new Wallet();
                 wallet1.setAmount(wallet.getAmount()-amount);
               int wal=  walletMapper.update(wallet1, updateWrapper); // 更新数据

                if(wal>0){
                    //用户消费100元
                    OrderItems orderItems = new OrderItems();
                    orderItems.setAmount(amount);
                    //生成订单编号
                    String no="C00"+System.currentTimeMillis();
                    orderItems.setNumber(no);
                    orderItems.setStatus(1);
                    orderItems.setMerchantAccount(merchantAccount);
                    orderItems.setUserAccount(userAccount);
                    int orderResult=orderItemsMapper.insert(orderItems);
                    if(orderResult>0){
                        //插入数据到用户钱包出账表
                        COrders cOrders = new COrders();
                        cOrders.setAmount(amount);
                        cOrders.setNumber(no);
                        cOrders.setUserAccount(userAccount);
                        int cOrderResult=cOrdersMapper.insert(cOrders);
                        if (cOrderResult>0){
                            jsonUtil.setCode(0);
                            jsonUtil.setMsg("用户消费成功");
                            jsonUtil.setData(amount);
                            return jsonUtil;
                        }else {
                            jsonUtil.setCode(1);
                            jsonUtil.setMsg("出错了");
                            return jsonUtil;
                        }
                    }else {
                        jsonUtil.setCode(1);
                        jsonUtil.setMsg("出错了");
                        return jsonUtil;
                    }
                }else {
                    jsonUtil.setCode(1);
                    jsonUtil.setMsg("出错了");
                    return jsonUtil;
                }


            }else {
                jsonUtil.setCode(1);
                jsonUtil.setMsg("该用户账号余额不足");
                return jsonUtil;
            }

        }else {
            jsonUtil.setCode(1);
            jsonUtil.setMsg("该用户账号不存在");
            return jsonUtil;
        }
    }


    /**
     * 用户退款20元的service 同上
     * @param userAccount 用户账号
     * @param amount 退款金额
     * @param merchantAccount 商户账号 可以选择对应商家账号对应的银行账户进行转账
     * @return  结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public JsonUtil AccRefund(String userAccount, Double amount, String merchantAccount) {
        JsonUtil jsonUtil = new JsonUtil();
        //判断用户账号是否存在
        List<Account> userList = accountMapper.selectList(
                new QueryWrapper<Account>()
                        .select("user_account")
                        .eq("user_account",userAccount)
                        .eq("del_flag",0));
        //商户账号有专用表，可以有对应的接口，暂不做校验
        if(userList!=null && userList.size()>0){
            //用户存在判断账号余额
            Wallet wallet = walletMapper.selectOne(
                    new QueryWrapper<Wallet>()
                            .select("amount")
                            .eq("user_account",userAccount)
                            .eq("status",1)
            );
            if(wallet.getAmount() > amount){
                //账户余额充足
                UpdateWrapper<Wallet> updateWrapper = new UpdateWrapper<>();
                updateWrapper
                        .eq("user_account", userAccount)
                        .eq("status",1); // 设置更新条件
                Wallet wallet1 = new Wallet();
                wallet1.setAmount(wallet.getAmount()+amount);
                int wal=  walletMapper.update(wallet1, updateWrapper); // 更新数据
                if(wal>0){
                    //用户退款20元
                    OrderItems orderItems = new OrderItems();
                    orderItems.setAmount(amount);
                    //生成订单编号
                    String no="R00"+System.currentTimeMillis();
                    orderItems.setNumber(no);
                    orderItems.setStatus(2);
                    orderItems.setMerchantAccount(merchantAccount);
                    orderItems.setUserAccount(userAccount);
                    int orderResult=orderItemsMapper.insert(orderItems);
                    if(orderResult>0){
                        //插入数据到用户钱包出账表
                        ROrders rOrders = new ROrders();
                        rOrders.setAmount(amount);
                        rOrders.setNumber(no);
                        rOrders.setUserAccount(userAccount);
                        int rOrderResult=rOrdersMapper.insert(rOrders);
                        if (rOrderResult>0){
                            jsonUtil.setCode(0);
                            jsonUtil.setMsg("用户退款20元成功");
                            return jsonUtil;
                        }else {
                            jsonUtil.setCode(1);
                            jsonUtil.setMsg("出错了");
                            return jsonUtil;
                        }
                    }else {
                        jsonUtil.setCode(1);
                        jsonUtil.setMsg("出错了");
                        return jsonUtil;
                    }
                }else {
                    jsonUtil.setCode(1);
                    jsonUtil.setMsg("出错了");
                    return jsonUtil;
                }


            }else {
                jsonUtil.setCode(1);
                jsonUtil.setMsg("该用户账号余额不足");
                return jsonUtil;
            }

        }else {
            jsonUtil.setCode(1);
            jsonUtil.setMsg("该用户账号不存在");
            return jsonUtil;
        }
    }

    @Override
    public JsonUtil accountDetailed(String userAccount) {
        JsonUtil jsonUtil = new JsonUtil();
        //判断用户账号是否存在
        List<Account> userList = accountMapper.selectList(
                new QueryWrapper<Account>()
                        .select("user_account")
                        .eq("user_account",userAccount)
                        .eq("del_flag",0));
        if(userList!=null && userList.size()>0){
            List<OrderItems> orderItems = orderItemsMapper.selectList(
                    new QueryWrapper<OrderItems>()
                            .eq("user_account",userAccount));

            jsonUtil.setCode(0);
            jsonUtil.setMsg("查询成功");
            jsonUtil.setData(orderItems);
            return jsonUtil;

        }else {
            jsonUtil.setCode(1);
            jsonUtil.setMsg("该用户账号不存在");
            return jsonUtil;
        }

    }
}
